// pages/新建项目/新建项目.js
Page({
  data: {
    project_data: {
      title: '',
      project_description: '',
      classification: '无',
      date: '',
      time: '',
      expected_lasting_time: '',
      reward: '',
      candidate_description: '',
      max_number: '',
      contact_name: '',
      contact_licence_number: '',
      contact_infomation: '',
      project_creator_id: ''
    },
    select: 0
  },

  onLoad(event){
    const app = getApp()
    this.setData({
      'project_data.project_creator_id': app.globalData.user_id
    })
  },

  blank(event){
    this.setData({
      select: 0
    })
  },

  title(event) {
    this.setData({
      'project_data.title': event.detail.value
    })
  },
  
  project_description(event) {
    this.setData({
      'project_data.project_description': event.detail.value
    })
  },

  date(event) {
    this.setData({
      'project_data.date': event.detail.value
    })
  },

  time(event) {
    this.setData({
      'project_data.time': event.detail.value
    })
  },

  expected_lasting_time(event) {
    this.setData({
      'project_data.expected_lasting_time': event.detail.value
    })
  },

  reward(event) {
    this.setData({
      'project_data.reward': event.detail.value
    })
  },

  candidate_description(event) {
    this.setData({
      'project_data.candidate_description': event.detail.value
    })
  },

  max_number(event) {
    this.setData({
      'project_data.max_number': event.detail.value.toString()
    })
  }, 

  contact_name(event) {
    this.setData({
      'project_data.contact_name': event.detail.value
    })
  }, 

  contact_licence_number(event) {
    this.setData({
      'project_data.contact_licence_number': event.detail.value.toString()
    })
  }, 

  contact_infomation(event) {
    this.setData({
      'project_data.contact_infomation': event.detail.value.toString()
    })
  }, 

  confirm(event) {
    wx.showModal({
      title: '新建项目',
      content: '确认项目相关信息填写吗？',
      complete: (res) => {
        const app = getApp()
        if (res.confirm) {
          wx.request({
            url: app.globalData.root_url + 'create_project/',
            method: 'POST',
            data: JSON.stringify(this.data.project_data),
            success(res) {
              if (res.data === 1) {
                wx.showModal({
                  title: '新建项目',
                  content: '新建成功',
                  showCancel: false,
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateBack()
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  },

  show_detail(event){
    this.setData({
      select: 1 - this.data.select
    })
  },

  choose_classification(event) {
    this.setData({
      'project_data.classification': event.currentTarget.dataset.classification,
      select: 0
    })
  }
})